﻿CREATE PROCEDURE [dbo].[DeleteLogInsert]
(
	@Id int OUTPUT,
	@TableName varchar(50),
	@IdValue int,
	@NotFound bit,
	@Deleted bit,
	@RejectReason varchar(250),
	@Note varchar(250),
	@MD datetime,
	@MW varchar(50)
)
AS
DECLARE @strMsg nvarchar(500), @intRet int, @intErrCode int
	SET NOCOUNT ON

IF (@TableName Is Null) OR (@IdValue Is Null)
BEGIN
	SELECT @intErrCode = 1, @strMsg = 'Неправильные параметры в вызове процедуры ' + Object_Name(@@Procid) + ' !'
	RAISERROR (@strMsg, 11, @intErrCode)
	RETURN @intErrCode
END

IF @NotFound Is Null SET @NotFound = 0
IF @Deleted Is Null SET @Deleted = 0

EXEC [dbo].[ProcessStatistic] @Note OUTPUT, @MD OUTPUT, @MW OUTPUT
--получение значения идентификатора
EXEC @intRet = dbo.GetNextCounterFor 'Common', @Id OUTPUT
IF @intRet <> 0 RETURN @intRet

	SET NOCOUNT OFF;
INSERT INTO [PR].[DeleteLog] 
([Id],[TableName],[IdValue],[NotFound],[Deleted],[RejectReason],[Note],[MD],[MW])
VALUES (@Id, @TableName, @IdValue, @NotFound, @Deleted, @RejectReason, @Note, @MD, @MW);
	
SELECT [Id],[TableName],[IdValue],[NotFound],[Deleted],[RejectReason],[Note],[MD],[MW]
FROM [PR].[DeleteLog] WHERE (Id = @Id)

RETURN 0
